import sqlite3
from datetime import datetime

def create_connection(db_file):
    """ 创建数据库连接到 SQLite 数据库 """
    conn = None
    try:
        conn = sqlite3.connect(db_file)
        print(f"数据库连接成功，SQLite 版本: {sqlite3.version}")
    except sqlite3.Error as e:
        print(f"连接数据库时出错: {e}")
    return conn

def select_all_users(conn):
    """ 查询所有用户 """
    try:
        cursor = conn.cursor()
        cursor.execute("SELECT * FROM workflow_instances")
        rows = cursor.fetchall()
        print("\n所有用户数据:")
        for row in rows:
            print(row)
    except sqlite3.Error as e:
        print(f"查询数据时出错: {e}")


def del_all(conn):
    """ 查询所有用户 """
    try:
        cursor = conn.cursor()
        cursor.execute("delete FROM workflow_instances")
        cursor.execute("delete FROM workflow_node_executions")
        cursor.execute("delete FROM workflow_logs")
        conn.commit()
    except sqlite3.Error as e:
        print(f"查询数据时出错: {e}")


import json
import yaml

def json_to_yaml(json_str):
    """
    将 JSON 字符串转换为 YAML 字符串
    """
    try:
        data = json.loads(json_str)
        
        yaml_str = yaml.dump(data, allow_unicode=True, default_flow_style=False, sort_keys=False)
        
        return yaml_str
    except json.JSONDecodeError as e:
        return f"JSON 解析错误: {e}"
    except Exception as e:
        return f"转换错误: {e}"

def main():
    # 数据库文件名（在当前目录下创建）
    database = "app.db"

    # 创建数据库连接
    conn = create_connection(database)

    if conn is not None:
        select_all_users(conn)
        # del_all(conn)
        # 关闭连接
        conn.close()
        print("\n数据库连接已关闭。")
    else:
        print("无法创建数据库连接。")

# 运行程序
if __name__ == '__main__':
    main()